package com.jeesite.modules.stock.dao;

import java.util.Comparator;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.stock.dao.mapper.UserColumnsMapper;
import com.jeesite.modules.stock.entity.UserColumnsEntity;

@Component
public class UserColumnsDao {

    @Autowired
    private UserColumnsMapper userColumnsMapper;

    /**
     * 查询用户的字段配置
     *
     * @param userId 用户id
     * @return 结果按sn排序
     */
    public List<UserColumnsEntity> findUserColumns(String userId) {
        UserColumnsEntity entity = new UserColumnsEntity();
        entity.setUserId(userId);
        List<UserColumnsEntity> list = userColumnsMapper.findList(entity);
        list.sort(Comparator.comparing(UserColumnsEntity::getSn));
        return list;
    }

    public void updateBatch(List<UserColumnsEntity> userColumns) {
        userColumnsMapper.updateBatch(userColumns);
    }

    public void insertBatch(List<UserColumnsEntity> userColumns){
        userColumnsMapper.insertBatch(userColumns);
    }

    public int count(String userId) {
        UserColumnsEntity entity = new UserColumnsEntity();
        entity.setUserId(userId);
        long count = userColumnsMapper.findCount(entity);
        return (int) count;
    }

    public boolean exist(String userId, String columnKey) {
        UserColumnsEntity entity = new UserColumnsEntity();
        entity.setUserId(userId);
        entity.setColumnKey(columnKey);
        long count = userColumnsMapper.findCount(entity);
        return count > 0;
    }

    public UserColumnsEntity findUserColumn(String userId, String columnKey) {
        UserColumnsEntity entity = new UserColumnsEntity();
        entity.setUserId(userId);
        entity.setColumnKey(columnKey);
        return userColumnsMapper.getByEntity(entity);
    }

    public UserColumnsEntity findUserColumnByDataIndex(String userId, String columnDataindex) {
        UserColumnsEntity entity = new UserColumnsEntity();
        entity.setUserId(userId);
        entity.setColumnDataindex(columnDataindex);
        return userColumnsMapper.getByEntity(entity);
    }

    /**
     * 删除用户的字段配置
     *
     * @param userId 用户id
     */
    public void deleteUserColumns(String userId) {
        if (!StringUtils.isBlank(userId)) {
            userColumnsMapper.deleteByUserId(userId);
        }
    }
    public void deleteUserColumn(String userId, String columnKey) {

    }

    public void insert(UserColumnsEntity entity) {
        userColumnsMapper.insert(entity);
    }

    public void update(UserColumnsEntity entity) {
        userColumnsMapper.update(entity);
    }
}
